package com.example.fengsam.aidong.activity;

import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.TimePicker;

import com.example.fengsam.aidong.R;
import com.example.fengsam.aidong.utils.SharedPreferencesUtils;
import com.jaeger.library.StatusBarUtil;
import com.lichfaker.scaleview.BaseScaleView;
import com.lichfaker.scaleview.HorizontalScaleScrollView;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SetPlanActivity extends BaseActivity {


    @BindView(R.id.iv_set_plan_back)
    ImageView ivSetPlanBack;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.layout_titlebar)
    LinearLayout layoutTitlebar;
    @BindView(R.id.tv_set_plan_step_number)
    TextView tvSetPlanStepNumber;
    @BindView(R.id.cb_set_plan_remind)
    CheckBox cbSetPlanRemind;
    @BindView(R.id.tv_set_plan_remind_time)
    TextView tvSetPlanRemindTime;
    @BindView(R.id.btn_set_plan_save)
    TextView btnSetPlanSave;
    @BindView(R.id.wv_set_plan)
    WebView wvSetPlan;

    private SharedPreferencesUtils sp;
    private String walk_qty;
    private String remind;
    private String achieveTime;
    private PopupWindow mPopWindow;

//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        initSwipeBackFinish();
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_set_plan);
//        ButterKnife.bind(this);
//
//        StatusBarUtil.setColor(this, getResources().getColor(R.color.colorPrimary));
//
//        initData();
//
//    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_set_plan);
        ButterKnife.bind(this);

        StatusBarUtil.setColor(this, getResources().getColor(R.color.colorPrimary));

        initData();
    }

    public void initData() {//获取锻炼计划
        sp = new SharedPreferencesUtils(this);
        String planWalk_QTY = (String) sp.getParam("planWalk_QTY", "7000");
        String remind = (String) sp.getParam("remind", "1");
        String achieveTime = (String) sp.getParam("achieveTime", "20:00");
        if (!planWalk_QTY.isEmpty()) {
            if ("0".equals(planWalk_QTY)) {
                tvSetPlanStepNumber.setText("7000");
            } else {
                tvSetPlanStepNumber.setText(planWalk_QTY);
            }
        }
        if (!remind.isEmpty()) {
            if ("0".equals(remind)) {
                cbSetPlanRemind.setChecked(false);
            } else if ("1".equals(remind)) {
                cbSetPlanRemind.setChecked(true);
            }
        }

        if (!achieveTime.isEmpty()) {
            tvSetPlanRemindTime.setText(achieveTime);
        }
    }

    @OnClick({R.id.iv_set_plan_back,R.id.btn_set_plan_save,R.id.tv_set_plan_remind_time,R.id.tv_set_plan_step_number})
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.iv_set_plan_back:
                finish();
                break;
            case R.id.btn_set_plan_save:
                save();
                break;
            case R.id.tv_set_plan_remind_time:
                showTimeDialog();
                break;
            case R.id.tv_set_plan_step_number:
                setStepNumber();
                break;
        }
    }

    private void save() {
        walk_qty = tvSetPlanStepNumber.getText().toString().trim();
//        remind = "";
        if (cbSetPlanRemind.isChecked()) {
            remind = "1";
        } else {
            remind = "0";
        }
        achieveTime = tvSetPlanRemindTime.getText().toString().trim();
        if (walk_qty.isEmpty() || "0".equals(walk_qty)) {
            sp.setParam("planWalk_QTY", "7000");
        } else {
            sp.setParam("planWalk_QTY", walk_qty);
        }
        sp.setParam("remind", remind);

        if (achieveTime.isEmpty()) {
            sp.setParam("achieveTime", "21:00");
            this.achieveTime = "21:00";
        } else {
            sp.setParam("achieveTime", achieveTime);
        }
        finish();
    }

    private void showTimeDialog() {
        final Calendar calendar = Calendar.getInstance(Locale.CHINA);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
//        String time = tv_remind_time.getText().toString().trim();
        final DateFormat df = new SimpleDateFormat("HH:mm");
//        Date date = null;
//        try {
//            date = df.parse(time);
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//
//        if (null != date) {
//            calendar.setTime(date);
//        }
        new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                calendar.set(Calendar.MINUTE, minute);
                String remaintime = calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE);
                Date date = null;
                try {
                    date = df.parse(remaintime);
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                if (null != date) {
                    calendar.setTime(date);
                }
                tvSetPlanRemindTime.setText(df.format(date));
            }
        }, hour, minute, true).show();
    }

    public void setStepNumber()
    {
        View contentView = LayoutInflater.from(this).inflate(R.layout.pv_set_plan, null);
        mPopWindow = new PopupWindow(contentView,
                WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
        mPopWindow.setContentView(contentView);
        //点击事件

        //显示PopupWindow
        View rootview = LayoutInflater.from(this).inflate(R.layout.activity_set_plan, null);
        mPopWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);

//        scaleScrollView.setOnScrollListener(new BaseScaleView.OnScrollListener() {
//            @Override
//            public void onScaleScroll(int scale) {
//                mTvHorizontalScale.setText("" + scale);
//            }
//        });

    }

}
